package com.whmall.service.crm.module.invoice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.invoice.OverseasLoanCompanyAddReqDTO;
import com.whmall.service.crm.entity.dto.invoice.OverseasLoanCompanyQueryReqDTO;
import com.whmall.service.crm.entity.dto.invoice.OverseasLoanCompanyRspDTO;
import com.whmall.service.crm.entity.dto.invoice.OverseasLoanCompanyUpdateReqDTO;
import com.whmall.service.crm.module.invoice.dao.OverseasLoanCompanyMapper;
import com.whmall.service.crm.module.invoice.entity.OverseasLoanCompanyDO;
import com.whmall.service.crm.module.invoice.service.OverseasLoanCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OverseasLoanCompanyServiceImpl implements OverseasLoanCompanyService {

    private static final String COUNTRY = "中国";

    @Autowired
    private OverseasLoanCompanyValidator validator;

    @Autowired
    private OverseasLoanCompanyMapper overseasLoanCompanyMapper;

    @Override
    public PageRspDTO<OverseasLoanCompanyRspDTO> query(OverseasLoanCompanyQueryReqDTO reqDTO, PageReqDTO pageReqDTO) {

        Assert.notNull(reqDTO, "参数异常");
        OverseasLoanCompanyQueryReqDTO queryReqDTO = validator.validateQuery(reqDTO);
        //按照创建时间排序
        queryReqDTO.setOrderBy("create_time desc");
        PageInfo<OverseasLoanCompanyDO> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(),
                pageReqDTO.getPageSize()).doSelectPageInfo(() -> overseasLoanCompanyMapper.selectBy(queryReqDTO));
        List<OverseasLoanCompanyRspDTO> overseasLoanCompanyRspDTOS = new ArrayList<>();
        if (CollUtil.isEmpty(pageInfo.getList())){
            return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                    pageInfo.getPages(), (int) pageInfo.getTotal(), overseasLoanCompanyRspDTOS);
        }
        pageInfo.getList().forEach(overseasLoanCompanyDO -> {
            OverseasLoanCompanyRspDTO overseasLoanCompanyRspDTO = new OverseasLoanCompanyRspDTO();
            BeanUtils.copyProperties(overseasLoanCompanyDO, overseasLoanCompanyRspDTO);
            overseasLoanCompanyRspDTOS.add(overseasLoanCompanyRspDTO);
        });
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                pageInfo.getPages(), (int) pageInfo.getTotal(), overseasLoanCompanyRspDTOS);
    }

    @Override
    @Transactional
    public Long create(OverseasLoanCompanyAddReqDTO reqDTO) {

        validator.validateAdd(reqDTO);
        OverseasLoanCompanyDO overseasLoanCompanyDO = new OverseasLoanCompanyDO();
        BeanUtils.copyProperties(reqDTO, overseasLoanCompanyDO);

        overseasLoanCompanyDO.setCreateId(reqDTO.getEmployeeId());
        overseasLoanCompanyDO.setCreateTime(new Date());
        //默认启用
        overseasLoanCompanyDO.setIsEnabled(1);
        overseasLoanCompanyDO.setEnableTime(new Date());
        overseasLoanCompanyDO.setEnableId(reqDTO.getEmployeeId());
        overseasLoanCompanyDO.setCountry(COUNTRY);
        overseasLoanCompanyMapper.insertSelective(overseasLoanCompanyDO);
        return overseasLoanCompanyDO.getId();
    }

    @Override
    @Transactional
    public void update(OverseasLoanCompanyUpdateReqDTO reqDTO) {

        validator.validateUpdate(reqDTO);
        OverseasLoanCompanyDO overseasLoanCompanyDO = overseasLoanCompanyMapper.selectById(reqDTO.getId());
        if (overseasLoanCompanyDO == null){
            throw new IllegalArgumentException("数据不存在");
        }
        BeanUtils.copyProperties(reqDTO, overseasLoanCompanyDO);
        overseasLoanCompanyDO.setUpdateId(reqDTO.getEmployeeId());
        overseasLoanCompanyDO.setUpdateTime(new Date());
        overseasLoanCompanyMapper.updateByIdSelective(overseasLoanCompanyDO);
    }

    @Override
    @Transactional
    public void enable(Long id, Long employeeId) {

        Assert.notNull(id, "海外货贷公司不能为空");

        OverseasLoanCompanyDO overseasLoanCompanyDO = overseasLoanCompanyMapper.selectById(id);
        if (overseasLoanCompanyDO == null){
            throw new IllegalArgumentException("数据异常");
        }
        Integer isEnable = overseasLoanCompanyDO.getIsEnabled();
        if (isEnable.equals(1)){
            overseasLoanCompanyDO.setIsEnabled(0);
        }else {
            overseasLoanCompanyDO.setIsEnabled(1);
        }
        overseasLoanCompanyDO.setEnableId(employeeId);
        overseasLoanCompanyDO.setEnableTime(new Date());
        overseasLoanCompanyMapper.updateByIdSelective(overseasLoanCompanyDO);
    }

    @Override
    public OverseasLoanCompanyRspDTO findByNameEn(String nameEn) {
        OverseasLoanCompanyRspDTO overseasLoanCompanyRspDTO = new OverseasLoanCompanyRspDTO();
        OverseasLoanCompanyDO overseasLoanCompanyDO = overseasLoanCompanyMapper.selectByNameEn(nameEn);
        if(Objects.nonNull(overseasLoanCompanyDO)){
            BeanUtils.copyProperties(overseasLoanCompanyDO, overseasLoanCompanyRspDTO);
        }else{
            overseasLoanCompanyDO = overseasLoanCompanyMapper.selectByNameEn("国内仓库");
            if(Objects.nonNull(overseasLoanCompanyDO)){
                BeanUtils.copyProperties(overseasLoanCompanyDO, overseasLoanCompanyRspDTO);
            }
        }
        return overseasLoanCompanyRspDTO;
    }

    @Override
    public List<OverseasLoanCompanyRspDTO> listByIds(List<Long> ids) {
        return overseasLoanCompanyMapper.selectBy(OverseasLoanCompanyQueryReqDTO.builder()
                .idIn(ids)
                .build()).stream()
                .map(e -> {
                    OverseasLoanCompanyRspDTO rspDTO = new OverseasLoanCompanyRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    return rspDTO;
                }).collect(Collectors.toList());
    }

    @Override
    public OverseasLoanCompanyRspDTO findById(Long id) {
        OverseasLoanCompanyDO overseasLoanCompanyDO = overseasLoanCompanyMapper.selectById(id);
        if (overseasLoanCompanyDO == null) {
            return null;
        }
        OverseasLoanCompanyRspDTO rspDTO = new OverseasLoanCompanyRspDTO();
        BeanUtils.copyProperties(overseasLoanCompanyDO, rspDTO);
        return rspDTO;
    }
}
